/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.MRP.bean.DemandManagementBeanRemote;
import Merlion.MRP.entity.MonthlySalesRecordEntity;
import Merlion.SCM.bean.InventoryItemsBeanRemote;
import Merlion.SCM.entity.ProductEntity;
import Merlion.util.state.InventoryState;
import Merlion.util.state.SalesRecordState;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author cute_jes91
 */
@ManagedBean(name="SalesRecordManagedBean")
@ViewScoped
public class SalesRecordManagedBean implements Serializable {
    private Long productId;
    private int monthInt;
    private int yearInt;
    private int actualBase;
    private int actualPromotion;
    private int actualSales;
    private SalesRecordState salesRecord;
    private List<SalesRecordState> salesRecords;
    private SalesRecordState[] salesRecordss;
    private List<InventoryState> products;
    private InventoryState newProduct;
    private List<MonthlySalesRecordEntity> monthlySalesRecords;
    private boolean converted;
    
    private MonthlySalesRecordEntity msre;
    @EJB
    private DemandManagementBeanRemote dmb;
    @EJB
    private InventoryItemsBeanRemote iib;

    /** Creates a new instance of SalesRecordManagedBean */
    public SalesRecordManagedBean() {
    }
    
    public void initView(PhaseEvent event){
        System.out.println("**SalesRecordManagedBean : initView()");
          
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
       
        //salesRecords= dmb.viewMonthlySalesRecords(null);
        products = iib.viewProducts();
    }

    public List<MonthlySalesRecordEntity> getMonthlySalesRecords() {
       //monthlySalesRecords.size();
        return monthlySalesRecords;
    }

    public void setMonthlySalesRecords(List<MonthlySalesRecordEntity> monthlySalesRecords) {
        this.monthlySalesRecords = monthlySalesRecords;
    }
    
    

    
    public void onEditRow(RowEditEvent event){
        System.out.println("**SalesRecordManagedBean: onEditRow()");

        try {
            SalesRecordState sr = (SalesRecordState) event.getObject();

            if (sr != null) {
                Long eProductId = sr.getProduct().getId();
                int eYear = sr.getYearInt();
                int eMonth = sr.getMonthInt();
                int eBase = sr.getActualBase();
                int ePromotion =sr.getActualPromotion();
                int eSales = eBase + ePromotion;
                sr.setActualSales(eSales);
                boolean eStatus = sr.isConverted();
                dmb.updateMonthlySalesRecord(eProductId, eYear, eMonth, eBase, ePromotion, eSales, eStatus);
                
                 System.out.println("**SalesRecordManagedBean :  sales record is successfully edited.");
            } else {
                System.out.println("**SalesRecordManagedBean: No sales record data found. onEditRow() failed.");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void doHandleUpdateProduct(){
         System.out.println("**SalesRecordManagedBean: doHandleUpdateProduct()");
         
         if(productId != null){
             List<SalesRecordState> srState = dmb.viewMonthlySalesRecordsByProduct(productId);
             this.setSalesRecords(srState);      
         }
        
    }
    
    public void doDelete(ActionEvent event){
         System.out.println("**SalesRecordManagedBean: doDelete()");

        try {
            if (salesRecord != null && salesRecord.isConverted()==false) {
                System.out.println("ID: "+salesRecord.getId());
                dmb.deleteMonthlySalesRecord(salesRecord.getId());
                System.out.println("**SalesRecordManagedBean: Deletion is successful.");
                FacesContext.getCurrentInstance().getExternalContext().redirect("SalesRecord.xhtml");
       
            } else { 
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Failed", "Sales Record ID "+ salesRecord.getId() +" has been CONVERTED so it cannot be deleted anymore" ));  
                System.out.println("**SalesRecordManagedBean: SalesRecord data is already converted.");
            }

             } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
    
    public void doDeletes(ActionEvent event){
        System.out.println("**SalesRecordManagedBean: doDeletes()");
     //BLOM DITAMBAHI CONSTRAINT KALO BLOM CONVERTED !!!
        try {
            if (salesRecordss != null) {
                for (SalesRecordState record : salesRecordss) {
                    dmb.deleteMonthlySalesRecord(record.getId());
                    System.out.println("**SalesRecordManagedBean: Successfully delete selected records.");
          
                }
            } else {
               // FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Failed", "Sales Record ID "+ salesRecord.getId() +" has been CONVERTED so it cannot be deleted anymore" ));  
                 
                System.out.println("**SalesRecordManagedBean: Selected Records are null.");
            }
            
            FacesContext.getCurrentInstance().getExternalContext().redirect("SalesRecord.xhtml");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void doAddNewRecord(ActionEvent event){
         System.out.println("**SalesRecordManagedBean: Create New Monthly Record.");     
        dmb.createMonthlySalesRecord(productId, yearInt, monthInt, actualBase, actualPromotion, actualSales);
         System.out.println("**SalesRecordManagedBean: A monthly record has been successfully added");
        try{
         FacesContext.getCurrentInstance().getExternalContext().redirect("SalesRecord.xhtml");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        
    }
    
    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public MonthlySalesRecordEntity getMsre() {
        return msre;
    }

    public void setMsre(MonthlySalesRecordEntity msre) {
        this.msre = msre;
    }

    public DemandManagementBeanRemote getDmb() {
        return dmb;
    }

    public void setDmb(DemandManagementBeanRemote dmb) {
        this.dmb = dmb;
    }

    public List<SalesRecordState> getSalesRecords() {
        return salesRecords;
    }

    public void setSalesRecords(List<SalesRecordState> salesRecords) {
        this.salesRecords = salesRecords;
    }

    public List<InventoryState> getProducts() {
        return products;
    }

    public void setProducts(List<InventoryState> products) {
        this.products = products;
    }

    public InventoryItemsBeanRemote getIib() {
        return iib;
    }

    public void setIib(InventoryItemsBeanRemote iib) {
        this.iib = iib;
    }

    public SalesRecordState[] getSalesRecordss() {
        return salesRecordss;
    }

    public void setSalesRecordss(SalesRecordState[] salesRecordss) {
       System.out.println("SET SALES RECORDSS");
        this.salesRecordss = salesRecordss;
    }

    public int getMonthInt() {
        return monthInt;
    }

    public void setMonthInt(int monthInt) {
        this.monthInt = monthInt;
    }

    public int getActualBase() {
        return actualBase;
    }

    public void setActualBase(int actualBase) {
        this.actualBase = actualBase;
    }

    public int getActualPromotion() {
        return actualPromotion;
    }

    public void setActualPromotion(int actualPromotion) {
        this.actualPromotion = actualPromotion;
    }

    public int getActualSales() {
        return actualSales;
    }

    public void setActualSales(int actualSales) {
        this.actualSales = actualSales;
    }

    public SalesRecordState getSalesRecord() {
        return salesRecord;
    }

    public void setSalesRecord(SalesRecordState salesRecord) {
        this.salesRecord = salesRecord;
    }

    public int getYearInt() {
        return yearInt;
    }

    public void setYearInt(int yearInt) {
        this.yearInt = yearInt;
    }

    public boolean isConverted() {
        return converted;
    }

    public void setConverted(boolean converted) {
        this.converted = converted;
    }
}
